home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Startup Screen Picker 1.2 / source / ssp startup app ƒ / Shell ƒ / prefs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  4.7 KB  |  218 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        prefs.c
  4.  
  5. Purpose:    This module handles the preferences file, which contains
  6.             the real filename of the current startup screen.
  7.             
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program in a file named "GNU General Public License".
  20. If not, write to the Free Software Foundation, 675 Mass Ave,
  21. Cambridge, MA 02139, USA.
  22.  
  23. \**********************************************************************/
  24.  
  25. #include "prefs.h"
  26. #include "environment.h"
  27. #include "program globals.h"
  28.  
  29. static PrefStruct    thePrefs;
  30. static long            gPrefsFilePos;
  31.  
  32. short PreferencesInit(void)
  33. {
  34.     short            prefsFileID;
  35.     short            err;
  36.     
  37.     err=OpenPrefsFile(&prefsFileID);
  38.     if (err!=prefs_allsWell)
  39.     {
  40.         if ((err==prefs_diskReadErr) || (err==prefs_diskWriteErr) || (err==prefs_virginErr))
  41.             ClosePrefsFile(prefsFileID);
  42.         return err;
  43.     }
  44.     
  45.     GetNextPrefs(prefsFileID);
  46.     if (err!=prefs_allsWell)
  47.     {
  48.         ClosePrefsFile(prefsFileID);
  49.         return err;
  50.     }
  51.     
  52.     CopyPrefsToGlobals();
  53.     ClosePrefsFile(prefsFileID);
  54.     
  55.     return prefs_allsWell;
  56. }
  57.  
  58. short OpenPrefsFile(short *prefsFileID)
  59. {
  60.     short            thisFile;
  61.     OSErr            isHuman;
  62.     short            vRefNum;
  63.     long            dirID;
  64.     FSSpec            prefsFile;
  65.     FInfo            prefsInfo;
  66.     short            temp;
  67.     long            count;
  68.     Boolean            newPrefs;
  69.     short            err;
  70.     unsigned char    *name=PREFS_FILE_NAME;
  71.     
  72.     newPrefs=FALSE;
  73.     isHuman=FindFolder(kOnSystemDisk, 'pref', kCreateFolder, &vRefNum, &dirID);
  74.     
  75.     if (isHuman!=noErr)
  76.         return prefs_cantOpenPrefsErr;
  77.  
  78.     if (gHasFSSpecs)
  79.     {
  80.         isHuman=FSMakeFSSpec(vRefNum, dirID, name, &prefsFile);
  81.         if (isHuman!=noErr)
  82.         {
  83.             if (isHuman==fnfErr)
  84.             {
  85.                 isHuman=FSpCreate(&prefsFile, CREATOR, PREFS_TYPE, 0);
  86.                 if (isHuman!=noErr)
  87.                     return prefs_cantCreatePrefsErr;
  88.                 newPrefs=TRUE;
  89.             }
  90.             else return prefs_cantOpenPrefsErr;
  91.         }
  92.         isHuman=FSpOpenDF(&prefsFile, fsRdWrPerm, &thisFile);
  93.         *prefsFileID=thisFile;
  94.         if (isHuman!=noErr)
  95.             return prefs_cantOpenPrefsErr;
  96.     }
  97.     else
  98.     {
  99.         isHuman=HOpen(vRefNum, dirID, name, fsRdWrPerm, &thisFile);
  100.         *prefsFileID=thisFile;
  101.         if (isHuman!=noErr)
  102.         {
  103.             if (isHuman==fnfErr)
  104.             {
  105.                 isHuman=HCreate(vRefNum, dirID, name, CREATOR, PREFS_TYPE);
  106.                 if (isHuman!=noErr)
  107.                     return prefs_cantCreatePrefsErr;
  108.                 prefsInfo.fdType=PREFS_TYPE;
  109.                 prefsInfo.fdCreator=CREATOR;
  110.                 prefsInfo.fdFlags=0;
  111.                 prefsInfo.fdLocation.h=prefsInfo.fdLocation.v=0;
  112.                 prefsInfo.fdFldr=0;
  113.                 isHuman=HSetFInfo(vRefNum, dirID, name, &prefsInfo);
  114.                 if (isHuman!=noErr)
  115.                     return prefs_cantCreatePrefsErr;
  116.                 isHuman=HOpen(vRefNum, dirID, name, fsRdWrPerm, &thisFile);
  117.                 *prefsFileID=thisFile;
  118.                 if (isHuman!=noErr)
  119.                     return prefs_cantOpenPrefsErr;
  120.                 newPrefs=TRUE;
  121.             }
  122.             else return prefs_cantOpenPrefsErr;
  123.         }
  124.     }
  125.     if (newPrefs)
  126.     {
  127.         return Virgin(*prefsFileID);
  128.     }
  129.     
  130.     return prefs_allsWell;
  131. }
  132.  
  133. void ClosePrefsFile(short prefsFileID)
  134. {
  135.     FSClose(prefsFileID);
  136.     FlushVol(0L, kOnSystemDisk);
  137. }
  138.  
  139. short GetNextPrefs(short prefsFileID)
  140. {
  141.     OSErr        isHuman;
  142.     long        count;
  143.     
  144.     count=sizeof(thePrefs);
  145.     isHuman=FSRead(prefsFileID, &count, &thePrefs);
  146.     if (isHuman!=noErr)
  147.         return prefs_diskReadErr;
  148.     
  149.     return prefs_allsWell;
  150. }
  151.  
  152. short SavePrefs(short prefsFileID)
  153. {
  154.     long        oldEOF;
  155.     OSErr        isHuman;
  156.     long        count;
  157.     
  158.     isHuman=SetEOF(prefsFileID, sizeof(thePrefs));
  159.     if (isHuman!=noErr)
  160.         return prefs_diskWriteErr;
  161.  
  162.     SetFPos(prefsFileID, 1, 0L);
  163.     count=sizeof(thePrefs);
  164.     isHuman=FSWrite(prefsFileID, &count, &thePrefs);
  165.     if (isHuman!=noErr)
  166.         return prefs_diskWriteErr;
  167.     
  168.     return prefs_allsWell;
  169. }
  170.  
  171. short Virgin(short prefsFileID)
  172. {
  173.     short            err;
  174.     
  175.     DefaultPrefs();
  176.     CopyGlobalsToPrefs();
  177.     err=SavePrefs(prefsFileID);
  178.     
  179.     return (err==prefs_allsWell) ? prefs_virginErr : err;
  180. }
  181.  
  182. void DefaultPrefs(void)
  183. {
  184.     unsigned char    *temp="\pOld StartupScreen";
  185.     short            i;
  186.     
  187.     for (i=temp[0]; i>=0; i--)
  188.         gLastName[i]=temp[i];
  189. }
  190.  
  191. void CopyGlobalsToPrefs(void)
  192. {
  193.     short            i;
  194.     
  195.     for (i=0; i<32; i++)
  196.         thePrefs.name[i]=0x00;
  197.     for (i=gLastName[0]; i>=0; i--)
  198.         thePrefs.name[i]=gLastName[i];
  199. }
  200.  
  201. void CopyPrefsToGlobals(void)
  202. {
  203.     short            i;
  204.     
  205.     for (i=thePrefs.name[0]; i>=0; i--)
  206.         gLastName[i]=thePrefs.name[i];
  207. }
  208.  
  209. void SaveThePrefs(void)
  210. {
  211.     short            prefsFileID;
  212.     
  213.     OpenPrefsFile(&prefsFileID);
  214.     CopyGlobalsToPrefs();
  215.     SavePrefs(prefsFileID);
  216.     ClosePrefsFile(prefsFileID);
  217. }
  218.